﻿
(function () {

    function Utility() {
    }

    Utility.prototype.GetCookie = function (c_name) {
        var i, x, y, cks = document.cookie.split(";");
        for (i = 0; i < cks.length; i++) {
            x = cks[i].substr(0, cks[i].indexOf("="));
            y = cks[i].substr(cks[i].indexOf("=") + 1);
            x = x.replace(/^\s+|\s+$/g, "");
            if (x == c_name) {
                return unescape(y);
            }
        }
        return null;
    };
    Utility.prototype.SetCookie = function (c_name, value, exdays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
        document.cookie = c_name + "=" + c_value;
    };
    Utility.prototype.getElementsByClassName = function (tagName, className) {
        var els = document.getElementsByTagName(tagName);
        for (var i = 0; i < els.length; i++) {
            var el = els[i];
            var elc = el.className ? el.className : el.getAttribute('class');

            if (elc &&
            (
                elc == className ||
                elc.indexOf(' ' + className + ' ') != -1 ||
                elc.indexOf(className + ' ') == 0 ||
                (elc.indexOf(' ' + className) != -1 && elc.indexOf(' ' + className) + className.length + 1 == elc.length))) {
                return el;
            }
        }
        return null;
    };

    var Utils = new Utility();
    window.Utils = Utils;
    //Utils.SetCookie('username', '小摘', 1);
    //Utils.SetCookie('cartcount', '5', 1);
    Utils.SetCookie('carttotal', '￥99.9', 1);

    function StatusHelper() {

    }

    StatusHelper.prototype.ShowName = function () {
        var ck = Utils.GetCookie('username');
        if (!ck || ck.length <= 0)
            return;

        var sText = '' +
'<span class="t_loginbar_span t_loginbar_span_hello">您好</span>' +
'<span class="t_loginbar_span t_loginbar_span_username">' + ck + '</span>' +
'<span class="t_loginbar_span t_loginbar_span_mark">！</span>' +
'<a class="t_loginbar_a t_loginbar_a_center" rel="nofollow" href="/member/default.aspx">[会员中心]</a><a class="t_loginbar_a t_loginbar_a_logout" rel="nofollow" href="/passport/logout.aspx">[退出]</a>';

        var el = Utils.getElementsByClassName('div', 't_loginbar');
        if (el) {
            el.innerHTML = sText;
        }
    };
    StatusHelper.prototype.ShowCartCount = function () {
        var ck = Utils.GetCookie('cartcount');
        if (!ck || ck.length <= 0)
            return;

        if (parseInt(ck) <= 0)
            return;

        var el = Utils.getElementsByClassName('span', 't_cartbar_span_number');
        el.setAttribute('class', el.getAttribute('class') + ' ' + 't_cartbar_span_numbers');
        if (el) {
            el.innerHTML = ck;
        }
    };

    StatusHelper.prototype.ShowCartTotal = function () {
        var ck = Utils.GetCookie('carttotal');
        if (!ck || ck.length <= 0)
            return;
        var el = Utils.getElementsByClassName('span', 't_cartbar_span_total');
        if (el)
            el.innerHTML = ck;
    };

    var ush = new StatusHelper();
    ush.ShowCartCount();
    ush.ShowName();
    ush.ShowCartTotal();
})();